// 修改右上方用户名
document.querySelector('#name').innerHTML = localStorage.getItem('username')

// 给退出按钮注册点击事件
const outBtn = document.querySelector('#logout')

// 退出登录的话清除 token 令牌并且返回到登录页面
outBtn.addEventListener('click', function () {

    location.href = '/login.html'
    localStorage.removeItem('token')
    localStorage.removeItem('username')
})


// 

const token = localStorage.getItem('token')

axios.get('/dashboard', {
    headers: {
        'Authorization': token
    }
}).then(({ data }) => {
    console.log(data)
    // 渲染就业薪资、班级人数、平均年龄、小组个数 数据
    document.querySelector('[name=salary]').innerHTML = data.data.overview.salary
    document.querySelector('[name=student_count]').innerHTML = data.data.overview.student_count
    document.querySelector('[name=age]').innerHTML = data.data.overview.age
    document.querySelector('[name=group_count]').innerHTML = data.data.overview.group_count

    // todo ================ 2021全学科薪资走势 =======================
    const myChart = echarts.init(document.getElementById('line'))

    // 指定图表的配置项和数据
    const option = {
        xAxis: {
            type: 'category',
            data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
            axisLine: {
                show: true,
                lineStyle: {
                    type: 'dashed'
                }
            },
        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: 'dashed',
                    color: ['#ccc']
                }
            }
        },
        series: [
            {
                // 遍历 year 数组
                data: data.data.year.map((item) => item.salary),
                type: 'line',
                smooth: true,
                symbol: 'emptyCircle',
                symbolSize: 10,
                lineStyle: {
                    width: 5,
                },
                // 区域颜色
                areaStyle: {
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [{
                            offset: 0, color: '#4d96ee' // 0% 处的颜色
                        }, {
                            offset: 0.8, color: 'rgba(255,255,255,0.1)' // 0% 处的颜色
                        }, {
                            offset: 1, color: 'rgba(255,255,255,0)' // 100% 处的颜色
                        }]
                    }
                }
            },
        ],
        title: {
            text: '2021全学科薪资走势',
            left: 10,
            top: 15,
            textStyle: {
                fontSize: 16,
            },
        },
        // 提示框组件
        tooltip: {
            show: true,
            trigger: 'axis',
        },
        // 刻度
        grid: {
            left: '10%',
            top: '20%',
        },
        // 折线颜色
        color: [
            {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 1,
                colorStops: [{
                    offset: 0, color: '#499FEE' // 0% 处的颜色
                }, {
                    offset: 1, color: '#5D75F0' // 100% 处的颜色
                }]
            },
        ],
    }

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option)



    // todo ==================== 薪资分布图 ==========================
    const myChart1 = echarts.init(document.getElementById('salary'))

    const option1 = {
        title: {
            text: '班级薪资分布',
            top: 15,
            left: 10,
            textStyle: {
                fontSize: 16,
            },
        },
        tooltip: {
            trigger: 'item'
        },
        legend: {
            top: '88%',
            left: 'center'
        },
        series: [
            {
                name: '班级薪资分布',
                type: 'pie',
                radius: ['50%', '64%'], // 圆的半径
                center: ['50%', '45%'], // 圆的中心点坐标
                itemStyle: {
                    borderRadius: 10,
                    borderColor: '#fff',
                    borderWidth: 2
                },
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: 40,
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: data.data.salaryData.map((item) => {
                    return {
                        value: item.g_count + item.b_count,
                        name: item.label,
                    }
                }),
            }
        ],
        color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A', '#ee6666'],
    };

    myChart1.setOption(option1)



    // todo ===================== 班级每组薪资 ========================
    const myChart2 = echarts.init(document.getElementById('lines'))

    const option2 = {
        grid: {
            left: 70,
            top: 30,
            right: 30,
            bottom: 50,
        },
        xAxis: {
            type: 'category',
            data: data.data.groupData[1].map(item => item.name),
            axisLine: {
                lineStyle: {
                    color: '#ccc',
                    type: 'dashed',
                },
            },
            axisLabel: {
                color: '#999',
            },
        },
        yAxis: {
            type: 'value',
            splitLine: {
                lineStyle: {
                    type: 'dashed',
                },
            },
        },
        tooltip: {
            trigger: 'item',
        },
        color: [
            {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                    {
                        offset: 0,
                        color: '#34D39A', // 0% 处的颜色
                    },
                    {
                        offset: 1,
                        color: 'rgba(52,211,154,0.2)', // 100% 处的颜色
                    },
                ],
            },
            {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                    {
                        offset: 0,
                        color: '#499FEE', // 0% 处的颜色
                    },
                    {
                        offset: 1,
                        color: 'rgba(73,159,238,0.2)', // 100% 处的颜色
                    },
                ],
            },
        ],
        series: [
            {
                data: data.data.groupData[1].map(item => item.hope_salary),
                type: 'bar',
                name: '期望薪资',
            },
            {
                data: data.data.groupData[1].map(item => item.salary),
                type: 'bar',
                name: '就业薪资',
            },
        ],
    }

    myChart2.setOption(option2)

    // 利用事件委托 注册点击事件
    const btns = document.querySelector('#btns')
    btns.addEventListener('click', function (e) {
        if (e.target.tagName === 'BUTTON') {
            // 找到btns 下面带有 btn-blue 类名的类 移除 btn-blue 类
            btns.querySelector('.btn-blue').classList.remove('btn-blue')
            // 给点击目标添加 btn-blue 类名，以此添加高亮效果
            e.target.classList.add('btn-blue')

            // 获取到每组的组号
            const group = e.target.innerText
            // console.log(group)
            option2.xAxis.data = data.data.groupData[group].map(item => item.name)
            option2.series[0].data = data.data.groupData[group].map(item => item.hope_salary)
            option2.series[1].data = data.data.groupData[group].map(item => item.salary)

            myChart2.setOption(option2)
        }
    })


    // todo ===================== 男女薪资分布 ======================
    const myChart3 = echarts.init(document.getElementById('gender'))

    const option3 = {
        title: [
            {
                text: '男女薪资分布',
                left: 10,
                top: 10,
                textStyle: {
                    fontSize: 16,
                },
            },
            {
                text: '男生',
                left: '50%',
                top: '45%',
                textAlign: 'center',
                textStyle: {
                    fontSize: 12,
                },
            },
            {
                text: '女生',
                left: '50%',
                top: '85%',
                textAlign: 'center',
                textStyle: {
                    fontSize: 12,
                },
            },
        ],
        color: ['#FDA224', '#5097FF', '#3ABCFA', '#34D39A'],
        tooltip: {
            trigger: 'item',
        },
        series: [
            {
                type: 'pie',
                radius: ['20%', '30%'],
                center: ['50%', '30%'],
                // data: [{name:'1万以下',value: 4},...]
                data: data.data.salaryData.map((item) => ({ name: item.label, value: item.b_count })),
            },
            {
                type: 'pie',
                radius: ['20%', '30%'],
                center: ['50%', '70%'],
                // data: [{name:'1万以下',value: 4},...]
                data: data.data.salaryData.map((item) => ({ name: item.label, value: item.g_count })),
            },
        ],
    }

    myChart3.setOption(option3)


    // todo ====================== 地图数据 ========================

        const myEchart4 = echarts.init(document.querySelector('#map'))
        const dataList = [
          { name: '南海诸岛', value: 0 },
          { name: '北京', value: 0 },
          { name: '天津', value: 0 },
          { name: '上海', value: 0 },
          { name: '重庆', value: 0 },
          { name: '河北', value: 0 },
          { name: '河南', value: 0 },
          { name: '云南', value: 0 },
          { name: '辽宁', value: 0 },
          { name: '黑龙江', value: 0 },
          { name: '湖南', value: 0 },
          { name: '安徽', value: 0 },
          { name: '山东', value: 0 },
          { name: '新疆', value: 0 },
          { name: '江苏', value: 0 },
          { name: '浙江', value: 0 },
          { name: '江西', value: 0 },
          { name: '湖北', value: 0 },
          { name: '广西', value: 0 },
          { name: '甘肃', value: 0 },
          { name: '山西', value: 0 },
          { name: '内蒙古', value: 0 },
          { name: '陕西', value: 0 },
          { name: '吉林', value: 0 },
          { name: '福建', value: 0 },
          { name: '贵州', value: 0 },
          { name: '广东', value: 0 },
          { name: '青海', value: 0 },
          { name: '西藏', value: 0 },
          { name: '四川', value: 0 },
          { name: '宁夏', value: 0 },
          { name: '海南', value: 0 },
          { name: '台湾', value: 0 },
          { name: '香港', value: 0 },
          { name: '澳门', value: 0 },
        ]
        dataList.forEach((item) => {
            const obj = data.data.provinceData.find((it) => it.name.replace(/省|回族自治区|吾尔自治区|壮族自治区|特别行政区|自治区/g, '') === item.name)
            if (obj) item.value = obj.value
          })
        let option4= {
          title: {
            text: '籍贯分布',
            top: 10,
            left: 10,
            textStyle: {
              fontSize: 16,
            },
          },
          tooltip: {
            trigger: 'item',
            formatter: '{b}: {c} 位学员',
            borderColor: 'transparent',
            backgroundColor: 'rgba(0,0,0,0.5)',
            textStyle: {
              color: '#fff',
            },
          },
          visualMap: {
            min: 0,
            max: 6,
            left: 'left',
            bottom: '20',
            text: ['6', '0'],
            inRange: {
              color: ['#ffffff', '#0075F0'],
            },
            show: true,
            left: 40,
          },
          geo: {
            map: 'china',
            roam: false,
            zoom: 1.0,
            label: {
              normal: {
                show: true,
                fontSize: '10',
                color: 'rgba(0,0,0,0.7)',
              },
            },
            itemStyle: {
              normal: {
                borderColor: 'rgba(0, 0, 0, 0.2)',
                color: '#e0ffff',
              },
              emphasis: {
                areaColor: '#34D39A',
                shadowOffsetX: 0,
                shadowOffsetY: 0,
                shadowBlur: 20,
                borderWidth: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)',
              },
            },
          },
          series: [
            {
              name: '籍贯分布',
              type: 'map',
              geoIndex: 0,
              data: dataList,
            },
          ],
        }
        myEchart4.setOption(option4)
      

})